package top.csmcool.musicapi.resource;

/*
 * @description
 * @date 2025/1/29
 * @author shijin
 * @version 1.0
 */

import cn.hutool.json.JSONObject;
import org.springframework.util.StringUtils;
import top.csmcool.musicapi.domain.UserPlatformBindInfo;
import top.csmcool.musicapi.domain.Users;
import top.csmcool.musicapi.model.api.navidrome.LyricLine;
import top.csmcool.musicapi.model.api.navidrome.SongCollection;
import top.csmcool.musicapi.model.playlist.*;
import top.csmcool.musicapi.service.UserPlatformBindInfoService;
import top.csmcool.musicapi.util.SpringUtils;
import top.csmcool.musicapi.util.TreadLocalUtil;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public interface MusicResource {

    public static final Pattern timeExp = Pattern.compile("\\[(\\d{2,}):(\\d{2})(?:\\.(\\d{2,3}))?\\]");

    /**
     * 只获取100条数据
     *
     * @param requestRequest 请求
     * @param users
     * @param one
     * @return list
     */
    default List<PlayListMusicInfo> getTracks(TracksRequest requestRequest, Users users, UserPlatformBindInfo one){
        return new LinkedList<>();
    };

    default List<LyricInfo> getLyric(LyricRequest body, Users users, UserPlatformBindInfo one) {
        return new LinkedList<>();
    };

     default List<SongCollection> getPlayList(PlayListRequest body, Users users, UserPlatformBindInfo one) {
         return new LinkedList<>();
    };

    default List<SongCollection> getPublicPlayList(PlayListRequest body, Users users, UserPlatformBindInfo one) {
        return new LinkedList<>();
    };

    default List<PlayListMusicInfo> search(SearchRequest body, Users users, UserPlatformBindInfo one) {
        return new LinkedList<>();
    };

    default UserPlatformBindInfo getUserPlatformBindInfo(Users users,
                                                         UserPlatformBindInfo one,
                                                         String platformUrl) {
        String username = users.getUsername();
        String password = users.getPassword();
        if(one != null) {
            if(!StringUtils.hasText(one.getPlatformUrl())) {
                one.setPlatformUrl(platformUrl);
            }
            if(!StringUtils.hasText(one.getUsername())) {
                one.setUsername(username);
            }
            if(!StringUtils.hasText(one.getPassword())) {
                one.setPassword(password);
            }
        } else {
            one = new UserPlatformBindInfo();
            one.setPlatformUrl(platformUrl);
            one.setUsername(username);
            one.setPassword(password);
        }
        return one;
    }

    default void updateCookie() {
        UserPlatformBindInfoService bean = SpringUtils.getBean(UserPlatformBindInfoService.class);
        UserPlatformBindInfo userPlatformBindInfo = TreadLocalUtil.getUserPlatformBindInfo();
        if(userPlatformBindInfo != null && StringUtils.hasText(userPlatformBindInfo.getCookie())) {
            UserPlatformBindInfo entity = new UserPlatformBindInfo();
            entity.setCookie(userPlatformBindInfo.getCookie());
            entity.setId(userPlatformBindInfo.getId());
            bean.updateById(entity);
        }
    }

    default List<LyricInfo> parseLyric(String lrc) {
        String[] lines = lrc.split("\n");
        List<LyricLine> lyric = new ArrayList<>();
        List<LyricInfo> lyricInfos = new LinkedList<>();
        for (String line : lines) {
            Matcher matcher = timeExp.matcher(line);
            if (!matcher.find()) {
                continue;
            }
            String text = line.replaceAll(timeExp.pattern(), "").trim();
            if (!text.isEmpty()) {
                int minutes = Integer.parseInt(matcher.group(1));
                int seconds = Integer.parseInt(matcher.group(2));
                int milliseconds = matcher.group(3) != null ? Integer.parseInt(matcher.group(3)) : 0;
                double time = minutes * 60 * 1000 + seconds * 1000  + milliseconds - 1500;
                lyric.add(new LyricLine(Double.valueOf(time).longValue(), text));
            }
        }

        for (LyricLine lyricLine : lyric) {
            LyricInfo lyricInfo = new LyricInfo();

            lyricInfo.setText(lyricLine.getValue());
            lyricInfo.setTime(lyricLine.getStart());

            lyricInfos.add(lyricInfo);
        }
        return lyricInfos;
    }

    default SongInfo getSongUrl(SongUrlRequest body, Users currentUser, UserPlatformBindInfo platformBindInfo){
        return null;
    };

    default JSONObject requestResource(JSONObject jsonObject, Users users, UserPlatformBindInfo one) {
        return null;
    }
}
